import React, {PropsWithChildren, useState} from "react"
import {PopoverProps} from "antd";
import {NumberIdAndName} from "../types/BasicModels";
import {getAllMarket} from "../apis/MarketsApi";
import SingleSelectConfirmPopover from "./SingleSelectConfirmPopover";

export interface MarketSelectPopoverProps extends PropsWithChildren, PopoverProps {
    onConfirm: (procurementSupplierId: number) => Promise<void>
}

const MarketSelectPopover: React.FC<MarketSelectPopoverProps> = (props) => {

    const [marketList, setMarketList] = useState<NumberIdAndName[]>([])

    let title = "选择卖场";

    return (
        <>
            <SingleSelectConfirmPopover title={title} onConfirm={props.onConfirm} options={marketList.map(it => ({
                label: it.name,
                value: it.id,
            }))} onRefresh={async ()=>{
                let response = await getAllMarket();
                setMarketList(response.data)
            }}>{props.children}</SingleSelectConfirmPopover>
        </>
    )
}

export default MarketSelectPopover
